package xin.tomdonkey.chat.netty.client;

import javafx.fxml.FXMLLoader;
import javafx.util.Callback;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author guolinyuan
 */
public class SpringFxmlLoader extends FXMLLoader
{
    @SuppressWarnings("rawtypes")
    public Object springLoad(String url)
    {
        try
        {
            InputStream fxmlStream = new ClassPathResource(url).getInputStream();
//            this.setControllerFactory(clazz -> Chat.context.getBean(clazz));
            this.setControllerFactory(new Callback<Class<?>, Object>()
            {
                @Override
                public Object call(Class<?> param)
                {
                    return Chat.context.getBean(param);
                }
            });
            return this.load(fxmlStream);
        }
        catch (IOException ioException)
        {
            throw new RuntimeException(ioException);
        }
    }
}
